<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  
  <title>RPC系列九之Thrift TProcessor源码分析 | Hexo</title>
  <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
  <meta name="description" content="简介　　本篇文章标题不是太准确啊，这里我们会介绍到通过thrift解析器生成的User和HelloWorldService的源码，并结合示例代码进行分析。事实上，每一个生成的HelloWorldService都会有一个TProcessor的实现。首先我们看看生成的User类
User　　下面仅仅列出最关键的代码。1234567891011121314151617181920212223242526">
<meta property="og:type" content="article">
<meta property="og:title" content="RPC系列九之Thrift TProcessor源码分析">
<meta property="og:url" content="http://yoursite.com/2015/12/01/rpc/9.RPC系列之Thrift TProcessor源码分析/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="简介　　本篇文章标题不是太准确啊，这里我们会介绍到通过thrift解析器生成的User和HelloWorldService的源码，并结合示例代码进行分析。事实上，每一个生成的HelloWorldService都会有一个TProcessor的实现。首先我们看看生成的User类
User　　下面仅仅列出最关键的代码。1234567891011121314151617181920212223242526">
<meta property="og:updated_time" content="2016-01-29T17:47:36.000Z">
<meta name="twitter:card" content="summary">
<meta name="twitter:title" content="RPC系列九之Thrift TProcessor源码分析">
<meta name="twitter:description" content="简介　　本篇文章标题不是太准确啊，这里我们会介绍到通过thrift解析器生成的User和HelloWorldService的源码，并结合示例代码进行分析。事实上，每一个生成的HelloWorldService都会有一个TProcessor的实现。首先我们看看生成的User类
User　　下面仅仅列出最关键的代码。1234567891011121314151617181920212223242526">
  
    <link rel="alternative" href="/atom.xml" title="Hexo" type="application/atom+xml">
  
  
    <link rel="icon" href="/favicon.png">
  
  <link href="//fonts.googleapis.com/css?family=Source+Code+Pro" rel="stylesheet" type="text/css">
  <link rel="stylesheet" href="/css/style.css" type="text/css">
  

</head>
<body>
  <div id="container">
    <div id="wrap">
      <header id="header">
  <div id="banner"></div>
  <div id="header-outer" class="outer">
    <div id="header-title" class="inner">
      <h1 id="logo-wrap">
        <a href="/" id="logo">Hexo</a>
      </h1>
      
    </div>
    <div id="header-inner" class="inner">
      <nav id="main-nav">
        <a id="main-nav-toggle" class="nav-icon"></a>
        
          <a class="main-nav-link" href="/">Home</a>
        
          <a class="main-nav-link" href="/archives">Archives</a>
        
      </nav>
      <nav id="sub-nav">
        
          <a id="nav-rss-link" class="nav-icon" href="/atom.xml" title="RSS Feed"></a>
        
        <a id="nav-search-btn" class="nav-icon" title="Search"></a>
      </nav>
      <div id="search-form-wrap">
        <form action="//google.com/search" method="get" accept-charset="UTF-8" class="search-form"><input type="search" name="q" results="0" class="search-form-input" placeholder="Search"><button type="submit" class="search-form-submit">&#xF002;</button><input type="hidden" name="sitesearch" value="http://yoursite.com"></form>
      </div>
    </div>
  </div>
</header>
      <div class="outer">
        <section id="main"><article id="post-rpc/9.RPC系列之Thrift TProcessor源码分析" class="article article-type-post" itemscope itemprop="blogPost">
  <div class="article-meta">
    <a href="/2015/12/01/rpc/9.RPC系列之Thrift TProcessor源码分析/" class="article-date">
  <time datetime="2015-12-01T01:00:00.000Z" itemprop="datePublished">2015-12-01</time>
</a>
    
  <div class="article-category">
    <a class="article-category-link" href="/categories/rpc/">rpc</a>
  </div>

  </div>
  <div class="article-inner">
    
    
      <header class="article-header">
        
  
    <h1 class="article-title" itemprop="name">
      RPC系列九之Thrift TProcessor源码分析
    </h1>
  

      </header>
    
    <div class="article-entry" itemprop="articleBody">
      
        <h2 id="u7B80_u4ECB"><a href="#u7B80_u4ECB" class="headerlink" title="简介"></a>简介</h2><p>　　本篇文章标题不是太准确啊，这里我们会介绍到通过thrift解析器生成的User和HelloWorldService的源码，并结合示例代码进行分析。事实上，每一个生成的HelloWorldService都会有一个TProcessor的实现。首先我们看看生成的User类</p>
<h2 id="User"><a href="#User" class="headerlink" title="User"></a>User</h2><p>　　下面仅仅列出最关键的代码。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br><span class="line">72</span><br><span class="line">73</span><br><span class="line">74</span><br><span class="line">75</span><br><span class="line">76</span><br><span class="line">77</span><br><span class="line">78</span><br><span class="line">79</span><br><span class="line">80</span><br><span class="line">81</span><br><span class="line">82</span><br><span class="line">83</span><br><span class="line">84</span><br><span class="line">85</span><br><span class="line">86</span><br><span class="line">87</span><br><span class="line">88</span><br><span class="line">89</span><br><span class="line">90</span><br><span class="line">91</span><br><span class="line">92</span><br><span class="line">93</span><br><span class="line">94</span><br><span class="line">95</span><br><span class="line">96</span><br><span class="line">97</span><br><span class="line">98</span><br><span class="line">99</span><br><span class="line">100</span><br><span class="line">101</span><br><span class="line">102</span><br><span class="line">103</span><br><span class="line">104</span><br><span class="line">105</span><br><span class="line">106</span><br><span class="line">107</span><br><span class="line">108</span><br><span class="line">109</span><br><span class="line">110</span><br><span class="line">111</span><br><span class="line">112</span><br><span class="line">113</span><br><span class="line">114</span><br><span class="line">115</span><br><span class="line">116</span><br><span class="line">117</span><br><span class="line">118</span><br><span class="line">119</span><br><span class="line">120</span><br><span class="line">121</span><br><span class="line">122</span><br><span class="line">123</span><br><span class="line">124</span><br><span class="line">125</span><br><span class="line">126</span><br><span class="line">127</span><br><span class="line">128</span><br><span class="line">129</span><br><span class="line">130</span><br><span class="line">131</span><br><span class="line">132</span><br><span class="line">133</span><br><span class="line">134</span><br><span class="line">135</span><br><span class="line">136</span><br><span class="line">137</span><br><span class="line">138</span><br><span class="line">139</span><br><span class="line">140</span><br><span class="line">141</span><br><span class="line">142</span><br><span class="line">143</span><br><span class="line">144</span><br><span class="line">145</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">User</span> <span class="keyword">implements</span> <span class="title">org</span>.<span class="title">apache</span>.<span class="title">thrift</span>.<span class="title">TBase</span>&lt;<span class="title">User</span>, <span class="title">User</span>.<span class="title">_Fields</span>&gt;, <span class="title">java</span>.<span class="title">io</span>.<span class="title">Serializable</span>, <span class="title">Cloneable</span>, <span class="title">Comparable</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> org.apache.thrift.protocol.TStruct STRUCT_DESC = <span class="keyword">new</span> org.apache.thrift.protocol.TStruct(<span class="string">"User"</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> org.apache.thrift.protocol.TField TYPE_FIELD_DESC = <span class="keyword">new</span> org.apache.thrift.protocol.TField(<span class="string">"type"</span>, org.apache.thrift.protocol.TType.I32, (<span class="keyword">short</span>)<span class="number">1</span>);</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> org.apache.thrift.protocol.TField NAME_FIELD_DESC = <span class="keyword">new</span> org.apache.thrift.protocol.TField(<span class="string">"name"</span>, org.apache.thrift.protocol.TType.STRING, (<span class="keyword">short</span>)<span class="number">2</span>);</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> org.apache.thrift.protocol.TField SEX_FIELD_DESC = <span class="keyword">new</span> org.apache.thrift.protocol.TField(<span class="string">"sex"</span>, org.apache.thrift.protocol.TType.STRING, (<span class="keyword">short</span>)<span class="number">3</span>);</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> org.apache.thrift.protocol.TField SCHOOL_FIELD_DESC = <span class="keyword">new</span> org.apache.thrift.protocol.TField(<span class="string">"school"</span>, org.apache.thrift.protocol.TType.STRING, (<span class="keyword">short</span>)<span class="number">4</span>);</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;Class&lt;? extends IScheme&gt;, SchemeFactory&gt; schemes = <span class="keyword">new</span> HashMap&lt;Class&lt;? extends IScheme&gt;, SchemeFactory&gt;();</span><br><span class="line">  <span class="keyword">static</span> &#123;</span><br><span class="line">    schemes.put(StandardScheme.class, <span class="keyword">new</span> UserStandardSchemeFactory());</span><br><span class="line">    schemes.put(TupleScheme.class, <span class="keyword">new</span> UserTupleSchemeFactory());</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">int</span> type; <span class="comment">// required</span></span><br><span class="line">  <span class="keyword">public</span> String name; <span class="comment">// optional</span></span><br><span class="line">  <span class="keyword">public</span> String sex; <span class="comment">// optional</span></span><br><span class="line">  <span class="keyword">public</span> String school; <span class="comment">// optional</span></span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">enum</span> _Fields implements org.apache.thrift.TFieldIdEnum &#123;</span><br><span class="line">    TYPE((<span class="keyword">short</span>)<span class="number">1</span>, <span class="string">"type"</span>),</span><br><span class="line">    NAME((<span class="keyword">short</span>)<span class="number">2</span>, <span class="string">"name"</span>),</span><br><span class="line">    SEX((<span class="keyword">short</span>)<span class="number">3</span>, <span class="string">"sex"</span>),</span><br><span class="line">    SCHOOL((<span class="keyword">short</span>)<span class="number">4</span>, <span class="string">"school"</span>);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;String, _Fields&gt; byName = <span class="keyword">new</span> HashMap&lt;String, _Fields&gt;();</span><br><span class="line"></span><br><span class="line">    <span class="keyword">static</span> &#123;</span><br><span class="line">      <span class="keyword">for</span> (_Fields field : EnumSet.allOf(_Fields.class)) &#123;</span><br><span class="line">        byName.put(field.getFieldName(), field);</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> _Fields <span class="title">findByThriftId</span><span class="params">(<span class="keyword">int</span> fieldId)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">switch</span>(fieldId) &#123;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">1</span>: <span class="comment">// TYPE</span></span><br><span class="line">          <span class="keyword">return</span> TYPE;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">2</span>: <span class="comment">// NAME</span></span><br><span class="line">          <span class="keyword">return</span> NAME;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">3</span>: <span class="comment">// SEX</span></span><br><span class="line">          <span class="keyword">return</span> SEX;</span><br><span class="line">        <span class="keyword">case</span> <span class="number">4</span>: <span class="comment">// SCHOOL</span></span><br><span class="line">          <span class="keyword">return</span> SCHOOL;</span><br><span class="line">        <span class="keyword">default</span>:</span><br><span class="line">          <span class="keyword">return</span> <span class="keyword">null</span>;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">	<span class="comment">//findByThriftIdOrThrow findByName等方法</span></span><br><span class="line"></span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> <span class="keyword">short</span> _thriftId;</span><br><span class="line">    <span class="keyword">private</span> <span class="keyword">final</span> String _fieldName;</span><br><span class="line"></span><br><span class="line">    _Fields(<span class="keyword">short</span> thriftId, String fieldName) &#123;</span><br><span class="line">      _thriftId = thriftId;</span><br><span class="line">      _fieldName = fieldName;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="comment">// isset id assignments</span></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> <span class="keyword">int</span> __TYPE_ISSET_ID = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">byte</span> __isset_bitfield = <span class="number">0</span>;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> _Fields optionals[] = &#123;_Fields.NAME,_Fields.SEX,_Fields.SCHOOL&#125;;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;_Fields, org.apache.thrift.meta_data.FieldMetaData&gt; metaDataMap;</span><br><span class="line">  <span class="keyword">static</span> &#123;</span><br><span class="line">    Map&lt;_Fields, org.apache.thrift.meta_data.FieldMetaData&gt; tmpMap = <span class="keyword">new</span> EnumMap&lt;_Fields, org.apache.thrift.meta_data.FieldMetaData&gt;(_Fields.class);</span><br><span class="line">    tmpMap.put(_Fields.TYPE, <span class="keyword">new</span> org.apache.thrift.meta_data.FieldMetaData(<span class="string">"type"</span>, org.apache.thrift.TFieldRequirementType.REQUIRED, </span><br><span class="line">        <span class="keyword">new</span> org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.I32)));</span><br><span class="line">    tmpMap.put(_Fields.NAME, <span class="keyword">new</span> org.apache.thrift.meta_data.FieldMetaData(<span class="string">"name"</span>, org.apache.thrift.TFieldRequirementType.OPTIONAL, </span><br><span class="line">        <span class="keyword">new</span> org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));</span><br><span class="line">    tmpMap.put(_Fields.SEX, <span class="keyword">new</span> org.apache.thrift.meta_data.FieldMetaData(<span class="string">"sex"</span>, org.apache.thrift.TFieldRequirementType.OPTIONAL, </span><br><span class="line">        <span class="keyword">new</span> org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));</span><br><span class="line">    tmpMap.put(_Fields.SCHOOL, <span class="keyword">new</span> org.apache.thrift.meta_data.FieldMetaData(<span class="string">"school"</span>, org.apache.thrift.TFieldRequirementType.OPTIONAL, </span><br><span class="line">        <span class="keyword">new</span> org.apache.thrift.meta_data.FieldValueMetaData(org.apache.thrift.protocol.TType.STRING)));</span><br><span class="line">    metaDataMap = Collections.unmodifiableMap(tmpMap);</span><br><span class="line">    org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(User.class, metaDataMap);</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="comment">//type等属性对应的unset set get方法 </span></span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> _Fields <span class="title">fieldForId</span><span class="params">(<span class="keyword">int</span> fieldId)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">return</span> _Fields.findByThriftId(fieldId);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">read</span><span class="params">(org.apache.thrift.protocol.TProtocol iprot)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">    schemes.get(iprot.getScheme()).getScheme().read(iprot, <span class="keyword">this</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(org.apache.thrift.protocol.TProtocol oprot)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">    schemes.get(oprot.getScheme()).getScheme().write(oprot, <span class="keyword">this</span>);</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">writeObject</span><span class="params">(java.io.ObjectOutputStream out)</span> <span class="keyword">throws</span> java.io.IOException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      write(<span class="keyword">new</span> org.apache.thrift.protocol.TCompactProtocol(<span class="keyword">new</span> org.apache.thrift.transport.TIOStreamTransport(out)));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (org.apache.thrift.TException te) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> java.io.IOException(te);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="function"><span class="keyword">private</span> <span class="keyword">void</span> <span class="title">readObject</span><span class="params">(java.io.ObjectInputStream in)</span> <span class="keyword">throws</span> java.io.IOException, ClassNotFoundException </span>&#123;</span><br><span class="line">    <span class="keyword">try</span> &#123;</span><br><span class="line">      <span class="comment">// it doesn't seem like you should have to do this, but java serialization is wacky, and doesn't call the default constructor.</span></span><br><span class="line">      __isset_bitfield = <span class="number">0</span>;</span><br><span class="line">      read(<span class="keyword">new</span> org.apache.thrift.protocol.TCompactProtocol(<span class="keyword">new</span> org.apache.thrift.transport.TIOStreamTransport(in)));</span><br><span class="line">    &#125; <span class="keyword">catch</span> (org.apache.thrift.TException te) &#123;</span><br><span class="line">      <span class="keyword">throw</span> <span class="keyword">new</span> java.io.IOException(te);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">UserStandardSchemeFactory</span> <span class="keyword">implements</span> <span class="title">SchemeFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserStandardScheme <span class="title">getScheme</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">new</span> UserStandardScheme();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">UserStandardScheme</span> <span class="keyword">extends</span> <span class="title">StandardScheme</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">read</span><span class="params">(org.apache.thrift.protocol.TProtocol iprot, User struct)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">      <span class="comment">//反序列化，根据读取到的字段id号，进行匹配</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(org.apache.thrift.protocol.TProtocol oprot, User struct)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">      <span class="comment">//非tuple协议的，均在此处序列化User，这里实际上设置的为序列化后字段对应的id号</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">UserTupleSchemeFactory</span> <span class="keyword">implements</span> <span class="title">SchemeFactory</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> UserTupleScheme <span class="title">getScheme</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">new</span> UserTupleScheme();</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">UserTupleScheme</span> <span class="keyword">extends</span> <span class="title">TupleScheme</span>&lt;<span class="title">User</span>&gt; </span>&#123;</span><br><span class="line"></span><br><span class="line">    <span class="annotation">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write</span><span class="params">(org.apache.thrift.protocol.TProtocol prot, User struct)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">      <span class="comment">//以tuple的格式进行序列化</span></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="annotation">@Override</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">read</span><span class="params">(org.apache.thrift.protocol.TProtocol prot, User struct)</span> <span class="keyword">throws</span> org.apache.thrift.TException </span>&#123;</span><br><span class="line">      <span class="comment">//将tuple格式的数据反序列化成User</span></span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　首先在User中最基础的便是TField类型的变量，每个属性（如name、sex等）都各自对应一个TField类型的static final属性，TField包含了该属性的名称，字段类型和属性id号。如type属性对应的Field如下（精简了包名）：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> TField TYPE_FIELD_DESC = <span class="keyword">new</span> TField(<span class="string">"type"</span>, TType.I32, (<span class="keyword">short</span>)<span class="number">1</span>);</span><br></pre></td></tr></table></figure></p>
<p>　　在User中还包含了一个静态代码块，会将user的元数据信息注册到org.apache.thrift.meta_data.FieldMetaData中，暂时没发现有地方使用过。<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">final</span> Map&lt;_Fields, org.apache.thrift.meta_data.FieldMetaData&gt; metaDataMap;</span><br><span class="line"><span class="keyword">static</span> &#123;</span><br><span class="line">  Map&lt;_Fields, FieldMetaData&gt; tmpMap = <span class="keyword">new</span> EnumMap&lt;_Fields, FieldMetaData&gt;(_Fields.class);</span><br><span class="line">  tmpMap.put(_Fields.TYPE, FieldMetaData(<span class="string">"type"</span>, TFieldRequirementType.REQUIRED, <span class="keyword">new</span> FieldValueMetaData(TType.I32)));</span><br><span class="line">  <span class="comment">//在放入其它的属性，如sex, name等。</span></span><br><span class="line">  org.apache.thrift.meta_data.FieldMetaData.addStructMetaDataMap(User.class, metaDataMap);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　还有一个内部枚举类_Fields，该枚举实现了接口TFieldIdEnum，每个枚举类型则包括了字段的id号和字段名称。最后也是最重要的便是对StandardScheme和TupleScheme的实现，他们定义了User类的序列化方式方式，详见上一篇文章介绍。</p>
<h2 id="HelloWorldService"><a href="#HelloWorldService" class="headerlink" title="HelloWorldService"></a>HelloWorldService</h2><p>　　下面我们进入HelloWorldService2的源码，梳理一下thrift为我们生成的HelloWorldService2的代码结构。首先可看接口的定义Iface和AsyncIface以及客户端涉及的代码Client和AsyncClient！<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">HelloWorldService2</span> </span>&#123;</span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">Iface</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String name)</span> <span class="keyword">throws</span> TException</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> <span class="class"><span class="keyword">interface</span> <span class="title">AsyncIface</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(String name, AsyncMethodCallback resultHandler)</span> <span class="keyword">throws</span> TException</span>;</span><br><span class="line">  &#125;</span><br><span class="line">  </span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Client</span> <span class="keyword">extends</span> <span class="title">TServiceClient</span> <span class="keyword">implements</span> <span class="title">Iface</span> </span>&#123;</span><br><span class="line">    <span class="comment">//省略构造方法和工厂类</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">sayHello</span><span class="params">(String name)</span> <span class="keyword">throws</span> TException</span>&#123;</span><br><span class="line">      send_sayHello(name);</span><br><span class="line">      <span class="keyword">return</span> recv_sayHello();</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">send_sayHello</span><span class="params">(String name)</span> <span class="keyword">throws</span> TException</span>&#123;</span><br><span class="line">      sayHello_args args = <span class="keyword">new</span> sayHello_args();</span><br><span class="line">      args.setName(name);</span><br><span class="line">      sendBase(<span class="string">"sayHello"</span>, args);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">recv_sayHello</span><span class="params">()</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">      sayHello_result result = <span class="keyword">new</span> sayHello_result();</span><br><span class="line">      receiveBase(result, <span class="string">"sayHello"</span>);</span><br><span class="line">      <span class="keyword">if</span> (result.isSetSuccess()) &#123;</span><br><span class="line">        <span class="keyword">return</span> result.success;</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">AsyncClient</span> <span class="keyword">extends</span> <span class="title">TAsyncClient</span> <span class="keyword">implements</span> <span class="title">AsyncIface</span> </span>&#123;</span><br><span class="line">    <span class="comment">//省略构造方法和工厂类</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">sayHello</span><span class="params">(String name, AsyncMethodCallback resultHandler)</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">      checkReady();</span><br><span class="line">      sayHello_call method_call = <span class="keyword">new</span> sayHello_call(name, resultHandler, <span class="keyword">this</span>, ___protocolFactory, ___transport);</span><br><span class="line">      <span class="keyword">this</span>.___currentMethod = method_call;</span><br><span class="line">      ___manager.call(method_call);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">sayHello_call</span> <span class="keyword">extends</span> <span class="title">TAsyncMethodCall</span> </span>&#123;</span><br><span class="line">      <span class="keyword">private</span> String name;</span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="title">sayHello_call</span><span class="params">(String name, AsyncMethodCallback resultHandler, TAsyncClient client, TProtocolFactory protocolFactory, TNonblockingTransport transport)</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">        <span class="keyword">super</span>(client, protocolFactory, transport, resultHandler, <span class="keyword">false</span>);</span><br><span class="line">        <span class="keyword">this</span>.name = name;</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">write_args</span><span class="params">(TProtocol prot)</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">        prot.writeMessageBegin(<span class="keyword">new</span> TMessage(<span class="string">"sayHello"</span>, TMessageType.CALL, <span class="number">0</span>));</span><br><span class="line">        sayHello_args args = <span class="keyword">new</span> sayHello_args();</span><br><span class="line">        args.setName(name);</span><br><span class="line">        args.write(prot);</span><br><span class="line">        prot.writeMessageEnd();</span><br><span class="line">      &#125;</span><br><span class="line"></span><br><span class="line">      <span class="function"><span class="keyword">public</span> String <span class="title">getResult</span><span class="params">()</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">        TMemoryInputTransport memoryTransport = <span class="keyword">new</span> TMemoryInputTransport(getFrameBuffer().array());</span><br><span class="line">        TProtocol prot = client.getProtocolFactory().getProtocol(memoryTransport);</span><br><span class="line">        <span class="keyword">return</span> (<span class="keyword">new</span> Client(prot)).recv_sayHello();</span><br><span class="line">      &#125;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　thrift为定义了HelloWorldService2两组接口：Iface、AsyncIface和Client、AsyncClient。其中Iface、AsyncIface定义了服务的接口sayHello，Client和AsyncClient则用于客户端的代码调用。其中Client方法最终会调用父类TServiceClient的sendBase方法，将请求进行序列化，写入流中。<br>　　服务端收到消息后，将请求交由HelloWorldService2的两个内部类Processor和AsyncProcessor执行，顾名思义，一个是同步调用，一个是异步调用<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">Processor</span>&lt;<span class="title">I</span> <span class="keyword">extends</span> <span class="title">Iface</span>&gt; <span class="keyword">extends</span> <span class="title">TBaseProcessor</span>&lt;<span class="title">I</span>&gt; <span class="keyword">implements</span> <span class="title">TProcessor</span> </span>&#123;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> <span class="keyword">final</span> Logger LOGGER = LoggerFactory.getLogger(Processor.class.getName());</span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">Processor</span><span class="params">(I iface)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">super</span>(iface, getProcessMap(<span class="keyword">new</span> HashMap&lt;String, org.apache.thrift.ProcessFunction&lt;I, ? extends org.apache.thrift.TBase&gt;&gt;()));</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> &lt;I extends Iface&gt; Map&lt;String, ProcessFunction&lt;I, ? extends TBase&gt;&gt; getProcessMap(Map&lt;String, ProcessFunction&lt;I, ? extends  TBase&gt;&gt; processMap) &#123;</span><br><span class="line">    processMap.put(<span class="string">"sayHello"</span>, <span class="keyword">new</span> sayHello());</span><br><span class="line">    <span class="keyword">return</span> processMap;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">sayHello</span>&lt;<span class="title">I</span> <span class="keyword">extends</span> <span class="title">Iface</span>&gt; <span class="keyword">extends</span> <span class="title">ProcessFunction</span>&lt;<span class="title">I</span>, <span class="title">sayHello_args</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">sayHello</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">super</span>(<span class="string">"sayHello"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> sayHello_result <span class="title">getResult</span><span class="params">(I iface, sayHello_args args)</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">      sayHello_result result = <span class="keyword">new</span> sayHello_result();</span><br><span class="line">      result.success = iface.sayHello(args.name);</span><br><span class="line">      <span class="keyword">return</span> result;</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　这里有一个非常巧妙的实现，在Processor内部类中存在一个名为sayHello的内部类，同时通过Processor.getProcessMap方法将名为sayHello的方法与名为sayHello的内部类建立了映射关系（放入了hashMap）。<br>　　当服务端获取到客户端调用sayhello方法的消息后，直接到hashMap中根据key=sayHello可以找到对应的value=sayHello内部类，并统一执行该内部类的getResult方法，而getResult方法又调用了Processor成员变量iface的sayHello方法。该成员变量在构造Processor对象是初始化。如下所示服务端的TProcessor初始化代码：<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">TProcessor tprocessor = <span class="keyword">new</span> HelloWorldService2.Processor&lt;HelloWorldService2.Iface&gt;(<span class="keyword">new</span> HelloWorldImpl2());</span><br></pre></td></tr></table></figure></p>
<p>　　通过这种方式，thrift规避了采用反射的方式执行，毕竟反射性能或多或少比直接调用的慢一点吧，神实现！！！最后我们简单了解一下AsyncProcessor的实现原理<br><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">AsyncProcessor</span>&lt;<span class="title">I</span> <span class="keyword">extends</span> <span class="title">AsyncIface</span>&gt; <span class="keyword">extends</span> <span class="title">TBaseAsyncProcessor</span>&lt;<span class="title">I</span>&gt; </span>&#123;</span><br><span class="line">  </span><br><span class="line">  <span class="function"><span class="keyword">public</span> <span class="title">AsyncProcessor</span><span class="params">(I iface)</span> </span>&#123;</span><br><span class="line">    <span class="keyword">super</span>(iface, getProcessMap(<span class="keyword">new</span> HashMap&lt;String, org.apache.thrift.AsyncProcessFunction&lt;I, ? extends org.apache.thrift.TBase, ?&gt;&gt;()));</span><br><span class="line">  &#125;</span><br><span class="line">  <span class="keyword">private</span> <span class="keyword">static</span> &lt;I extends AsyncIface&gt; Map&lt;String, AsyncProcessFunction&lt;I, ? extends TBase,?&gt;&gt; getProcessMap(Map&lt;String, AsyncProcessFunction&lt;I, ? extends TBase, ?&gt;&gt; processMap) &#123;</span><br><span class="line">    processMap.put(<span class="string">"sayHello"</span>, <span class="keyword">new</span> sayHello());</span><br><span class="line">    <span class="keyword">return</span> processMap;</span><br><span class="line">  &#125;</span><br><span class="line"></span><br><span class="line">  <span class="keyword">public</span> <span class="keyword">static</span> <span class="class"><span class="keyword">class</span> <span class="title">sayHello</span>&lt;<span class="title">I</span> <span class="keyword">extends</span> <span class="title">AsyncIface</span>&gt; <span class="keyword">extends</span> <span class="title">org</span>.<span class="title">apache</span>.<span class="title">thrift</span>.<span class="title">AsyncProcessFunction</span>&lt;<span class="title">I</span>, <span class="title">sayHello_args</span>, <span class="title">String</span>&gt; </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="title">sayHello</span><span class="params">()</span> </span>&#123;</span><br><span class="line">      <span class="keyword">super</span>(<span class="string">"sayHello"</span>);</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> AsyncMethodCallback&lt;String&gt; <span class="title">getResultHandler</span><span class="params">(<span class="keyword">final</span> AsyncFrameBuffer fb, <span class="keyword">final</span> <span class="keyword">int</span> seqid)</span> </span>&#123;</span><br><span class="line">      <span class="keyword">final</span> org.apache.thrift.AsyncProcessFunction fcall = <span class="keyword">this</span>;</span><br><span class="line">      <span class="keyword">return</span> <span class="keyword">new</span> AsyncMethodCallback&lt;String&gt;() &#123; </span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onComplete</span><span class="params">(String o)</span> </span>&#123;</span><br><span class="line">          sayHello_result result = <span class="keyword">new</span> sayHello_result();</span><br><span class="line">          result.success = o;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">            fcall.sendResponse(fb,result, org.apache.thrift.protocol.TMessageType.REPLY,seqid);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">          &#125; <span class="keyword">catch</span> (Exception e) &#123;</span><br><span class="line">            LOGGER.error(<span class="string">"Exception writing to internal frame buffer"</span>, e);</span><br><span class="line">          &#125;</span><br><span class="line">          fb.close();</span><br><span class="line">        &#125;</span><br><span class="line">        <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">onError</span><span class="params">(Exception e)</span> </span>&#123;</span><br><span class="line">          <span class="keyword">byte</span> msgType = org.apache.thrift.protocol.TMessageType.REPLY;</span><br><span class="line">          org.apache.thrift.TBase msg;</span><br><span class="line">          sayHello_result result = <span class="keyword">new</span> sayHello_result();</span><br><span class="line">          &#123;</span><br><span class="line">            msgType = org.apache.thrift.protocol.TMessageType.EXCEPTION;</span><br><span class="line">            msg = (org.apache.thrift.TBase)<span class="keyword">new</span> org.apache.thrift.TApplicationException(org.apache.thrift.TApplicationException.INTERNAL_ERROR, e.getMessage());</span><br><span class="line">          &#125;</span><br><span class="line">          <span class="keyword">try</span> &#123;</span><br><span class="line">            fcall.sendResponse(fb,msg,msgType,seqid);</span><br><span class="line">            <span class="keyword">return</span>;</span><br><span class="line">          &#125; <span class="keyword">catch</span> (Exception ex) &#123;</span><br><span class="line">            LOGGER.error(<span class="string">"Exception writing to internal frame buffer"</span>, ex);</span><br><span class="line">          &#125;</span><br><span class="line">          fb.close();</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;</span><br><span class="line"> </span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">start</span><span class="params">(I iface, sayHello_args args, org.apache.thrift.async.AsyncMethodCallback&lt;String&gt; resultHandler)</span> <span class="keyword">throws</span> TException </span>&#123;</span><br><span class="line">      iface.sayHello(args.name,resultHandler);</span><br><span class="line">    &#125;</span><br><span class="line">  &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></p>
<p>　　与同步AsyncProcessor执行getResult不同，AsyncMethodCallback执行的是start方法，在该方法中调用服务接口的对应方法，同时通过回调接口AsyncMethodCallback实现执行结果的异步返回。这里有一个细节需要注意：每次接口调用，都会带上seqid作为该次请求的唯一标识（自增形式），返回结果时也带上该值作为标识，匹配对应的请求。</p>
<h2 id="u603B_u7ED3"><a href="#u603B_u7ED3" class="headerlink" title="总结"></a>总结</h2><p>　　其实，TProcessor的实现原理主要在thrift为我们生成的服务类HelloWorldService中，理清了HelloWorldService的结构原理基本就OK了。再次强调一次，服务端接口的执行并不是采用反射的形式哦。<br>　　到这里Thrift就介绍完毕了。有几点需要注意的是：<br>　　1. thrift提供了与hessian类型的基于http的rpc调用，采用org.apache.thrift.server.TServlet和org.apache.thrift.transport.THttpClient搭配即可<br>　　2. thrift不支持接口的重载。<br>　　3. 提供了多种序列化方式和服务端实现，以及同步异步的调用</p>

      
    </div>
    <footer class="article-footer">
      <a data-url="http://yoursite.com/2015/12/01/rpc/9.RPC系列之Thrift TProcessor源码分析/" data-id="cijzzpv2i001r1cfslat98eo0" class="article-share-link">Share</a>
      
      
  <ul class="article-tag-list"><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/java/">java</a></li><li class="article-tag-list-item"><a class="article-tag-list-link" href="/tags/rpc/">rpc</a></li></ul>

    </footer>
  </div>
  
    
<nav id="article-nav">
  
    <a href="/2015/12/01/rpc/10.RPC系列之Thrift 异步调用/" id="article-nav-newer" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Newer</strong>
      <div class="article-nav-title">
        
          RPC系列十之Thrift 异步调用
        
      </div>
    </a>
  
  
    <a href="/2015/12/01/rpc/8.RPC系列之Thrift TServer源码分析/" id="article-nav-older" class="article-nav-link-wrap">
      <strong class="article-nav-caption">Older</strong>
      <div class="article-nav-title">RPC系列八之Thrift TServer源码分析</div>
    </a>
  
</nav>

  
</article>

</section>
        
          <aside id="sidebar">
  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Categories</h3>
    <div class="widget">
      <ul class="category-list"><li class="category-list-item"><a class="category-list-link" href="/categories/cache/">cache</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/database/">database</a><span class="category-list-count">13</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/docker/">docker</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gis/">gis</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/git/">git</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/go/">go</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/gradle/">gradle</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/groovy/">groovy</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/hadoop/">hadoop</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/java/">java</a><span class="category-list-count">18</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/linux/">linux</a><span class="category-list-count">4</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/log/">log</a><span class="category-list-count">11</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mongodb/">mongodb</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mq/">mq</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/mybatis/">mybatis</a><span class="category-list-count">9</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/netty/">netty</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nginx/">nginx</a><span class="category-list-count">3</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nio/">nio</a><span class="category-list-count">19</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/nodejs/">nodejs</a><span class="category-list-count">1</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/redis/">redis</a><span class="category-list-count">6</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/rpc/">rpc</a><span class="category-list-count">14</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/spring/">spring</a><span class="category-list-count">5</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/tomcat/">tomcat</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/zookeeper/">zookeeper</a><span class="category-list-count">2</span></li><li class="category-list-item"><a class="category-list-link" href="/categories/杂项/">杂项</a><span class="category-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tags</h3>
    <div class="widget">
      <ul class="tag-list"><li class="tag-list-item"><a class="tag-list-link" href="/tags/aop/">aop</a><span class="tag-list-count">8</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/aspectj/">aspectj</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/cache/">cache</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/classloader/">classloader</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/common-loggings/">common-loggings</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/commonjs/">commonjs</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/database/">database</a><span class="tag-list-count">10</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/docker/">docker</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/dubbo/">dubbo</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/fastdfs/">fastdfs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gclib/">gclib</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/geohash/">geohash</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gis/">gis</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/git/">git</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/go/">go</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/gradle/">gradle</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/groovy/">groovy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hadoop/">hadoop</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/hessisan/">hessisan</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/java/">java</a><span class="tag-list-count">48</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/javassist/">javassist</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/kafka/">kafka</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/linux/">linux</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log/">log</a><span class="tag-list-count">11</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/log4j/">log4j</a><span class="tag-list-count">4</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/logback/">logback</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/map/">map</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/memecached/">memecached</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mina/">mina</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mongodb/">mongodb</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mq/">mq</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mqtt/">mqtt</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/mybatis/">mybatis</a><span class="tag-list-count">9</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/netty/">netty</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nginx/">nginx</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nio/">nio</a><span class="tag-list-count">5</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/nodejs/">nodejs</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/postgresql/">postgresql</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/protobuf/">protobuf</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/proxy/">proxy</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/push/">push</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rabbitmq/">rabbitmq</a><span class="tag-list-count">3</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/redis/">redis</a><span class="tag-list-count">6</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/rpc/">rpc</a><span class="tag-list-count">14</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sitemesh/">sitemesh</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/sld/">sld</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/slf4j/">slf4j</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/socket/">socket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spatialite-for-android/">spatialite for android</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spi/">spi</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/spring/">spring</a><span class="tag-list-count">13</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/tomcat/">tomcat</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/transaction/">transaction</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/web/">web</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/xSocket/">xSocket</a><span class="tag-list-count">7</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zbus/">zbus</a><span class="tag-list-count">1</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/zookeeper/">zookeeper</a><span class="tag-list-count">2</span></li><li class="tag-list-item"><a class="tag-list-link" href="/tags/数据结构/">数据结构</a><span class="tag-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Tag Cloud</h3>
    <div class="widget tagcloud">
      <a href="/tags/aop/" style="font-size: 15.38px;">aop</a> <a href="/tags/aspectj/" style="font-size: 11.54px;">aspectj</a> <a href="/tags/cache/" style="font-size: 11.54px;">cache</a> <a href="/tags/classloader/" style="font-size: 10px;">classloader</a> <a href="/tags/common-loggings/" style="font-size: 10px;">common-loggings</a> <a href="/tags/commonjs/" style="font-size: 12.31px;">commonjs</a> <a href="/tags/database/" style="font-size: 16.92px;">database</a> <a href="/tags/docker/" style="font-size: 13.85px;">docker</a> <a href="/tags/dubbo/" style="font-size: 10px;">dubbo</a> <a href="/tags/fastdfs/" style="font-size: 10px;">fastdfs</a> <a href="/tags/gclib/" style="font-size: 10.77px;">gclib</a> <a href="/tags/geohash/" style="font-size: 10px;">geohash</a> <a href="/tags/gis/" style="font-size: 10px;">gis</a> <a href="/tags/git/" style="font-size: 10.77px;">git</a> <a href="/tags/go/" style="font-size: 10px;">go</a> <a href="/tags/gradle/" style="font-size: 10px;">gradle</a> <a href="/tags/groovy/" style="font-size: 10px;">groovy</a> <a href="/tags/hadoop/" style="font-size: 10px;">hadoop</a> <a href="/tags/hessisan/" style="font-size: 11.54px;">hessisan</a> <a href="/tags/java/" style="font-size: 20px;">java</a> <a href="/tags/javassist/" style="font-size: 10px;">javassist</a> <a href="/tags/kafka/" style="font-size: 11.54px;">kafka</a> <a href="/tags/linux/" style="font-size: 12.31px;">linux</a> <a href="/tags/log/" style="font-size: 17.69px;">log</a> <a href="/tags/log4j/" style="font-size: 12.31px;">log4j</a> <a href="/tags/logback/" style="font-size: 13.08px;">logback</a> <a href="/tags/map/" style="font-size: 10px;">map</a> <a href="/tags/memecached/" style="font-size: 10.77px;">memecached</a> <a href="/tags/mina/" style="font-size: 13.08px;">mina</a> <a href="/tags/mongodb/" style="font-size: 19.23px;">mongodb</a> <a href="/tags/mq/" style="font-size: 13.85px;">mq</a> <a href="/tags/mqtt/" style="font-size: 10.77px;">mqtt</a> <a href="/tags/mybatis/" style="font-size: 16.15px;">mybatis</a> <a href="/tags/netty/" style="font-size: 10px;">netty</a> <a href="/tags/nginx/" style="font-size: 11.54px;">nginx</a> <a href="/tags/nio/" style="font-size: 13.08px;">nio</a> <a href="/tags/nodejs/" style="font-size: 10px;">nodejs</a> <a href="/tags/postgresql/" style="font-size: 10.77px;">postgresql</a> <a href="/tags/protobuf/" style="font-size: 10px;">protobuf</a> <a href="/tags/proxy/" style="font-size: 10px;">proxy</a> <a href="/tags/push/" style="font-size: 10px;">push</a> <a href="/tags/rabbitmq/" style="font-size: 11.54px;">rabbitmq</a> <a href="/tags/redis/" style="font-size: 13.85px;">redis</a> <a href="/tags/rpc/" style="font-size: 19.23px;">rpc</a> <a href="/tags/sitemesh/" style="font-size: 10px;">sitemesh</a> <a href="/tags/sld/" style="font-size: 10px;">sld</a> <a href="/tags/slf4j/" style="font-size: 10px;">slf4j</a> <a href="/tags/socket/" style="font-size: 14.62px;">socket</a> <a href="/tags/spatialite-for-android/" style="font-size: 10px;">spatialite for android</a> <a href="/tags/spi/" style="font-size: 10px;">spi</a> <a href="/tags/spring/" style="font-size: 18.46px;">spring</a> <a href="/tags/tomcat/" style="font-size: 10.77px;">tomcat</a> <a href="/tags/transaction/" style="font-size: 10px;">transaction</a> <a href="/tags/web/" style="font-size: 10px;">web</a> <a href="/tags/xSocket/" style="font-size: 14.62px;">xSocket</a> <a href="/tags/zbus/" style="font-size: 10px;">zbus</a> <a href="/tags/zookeeper/" style="font-size: 10.77px;">zookeeper</a> <a href="/tags/数据结构/" style="font-size: 10.77px;">数据结构</a>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Archives</h3>
    <div class="widget">
      <ul class="archive-list"><li class="archive-list-item"><a class="archive-list-link" href="/archives/2016/01/">January 2016</a><span class="archive-list-count">67</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/12/">December 2015</a><span class="archive-list-count">81</span></li><li class="archive-list-item"><a class="archive-list-link" href="/archives/2015/11/">November 2015</a><span class="archive-list-count">2</span></li></ul>
    </div>
  </div>

  
    
  <div class="widget-wrap">
    <h3 class="widget-title">Recents</h3>
    <div class="widget">
      <ul>
        
          <li>
            <a href="/2016/01/30/资料/">(no title)</a>
          </li>
        
          <li>
            <a href="/2016/01/30/java/13.Classloader系列一/">ClassLoader系列一</a>
          </li>
        
          <li>
            <a href="/2016/01/29/hadoop/1.hadoop系列之源码编译/">hadoop系列一之源码编译</a>
          </li>
        
          <li>
            <a href="/2016/01/29/go/1.GO系列之入门/">Go系列一之入门</a>
          </li>
        
          <li>
            <a href="/2016/01/28/tomcat/2.tomcat性能优化/">tomcat性能优化</a>
          </li>
        
      </ul>
    </div>
  </div>

  
</aside>
        
      </div>
      <footer id="footer">
  
  <div class="outer">
    <div id="footer-info" class="inner">
      &copy; 2016 John Doe<br>
      Powered by <a href="http://hexo.io/" target="_blank">Hexo</a>
    </div>
  </div>
</footer>
    </div>
    <nav id="mobile-nav">
  
    <a href="/" class="mobile-nav-link">Home</a>
  
    <a href="/archives" class="mobile-nav-link">Archives</a>
  
</nav>
    


<script src="http://libs.baidu.com/jquery/2.0.3/jquery.min.js"></script>


  <link rel="stylesheet" href="/fancybox/jquery.fancybox.css" type="text/css">
  <script src="/fancybox/jquery.fancybox.pack.js" type="text/javascript"></script>


<script src="/js/script.js" type="text/javascript"></script>

  </div>
</body>
</html>